package com.example.demo.controller;

import com.example.demo.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/img")
public class ImageController {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping(value = "/getImageInfo")
    public ApiResponse getImageInfo(){
        Set<Object> imageMap = stringRedisTemplate.opsForHash().keys("imageMap");
        Iterator<Object> it = imageMap.iterator();
        List<PopularImage> list = new ArrayList<>();
        int i =0;
        while(it.hasNext()){
            String key = (String)it.next();
            String value = (String)stringRedisTemplate.opsForHash().get("imageMap", key);
            list.add(new PopularImage(key, value));
            i++;
        }
        return new ApiResponse(StatusCodeMenu.SUCCESS, StatusMessageMenu.SUCCESS, list);
    }

}
